﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using TemplateEngineWithChart.Docx;
using TemplateEngineWithChart.Docx.FullExample;
using TemplateEngineWithChart.Docx.TemplateCustomContent;
using ConsoleApp1.Template1;

namespace ConsoleApp1.Template2
{
    internal class InputTemplate2 : InputTemplate
    {
        public InputTemplate2()
        {
        }

        /// <summary>
        /// 职检基本信息
        /// </summary>
        public void SetOccuHM(OccHMInfo occHMInfo)
        {
            // 顶级属性
            if (occHMInfo != null && occHMInfo.DictOccHMInfo != null)
            {
                foreach (var item in occHMInfo.DictOccHMInfo)
                {
                    FieldContent fieldContent = new FieldContent(item.Key, item.Value);
                    fieldContent.IsHidden = string.IsNullOrEmpty(item.Value);
                    this.fValuesToFill.Fields.Add(fieldContent);
                }
            }
        }

        /// <summary>
        /// 职业健康检查汇总结果处理意见
        /// </summary>
        /// <param name="dt"></param>
        public void SetOrgSuggestion(DataTable dt)
        {
            // 职业禁忌证
            DataTable table1 = new DataTable();
            DataRow[] rows1 = dt.Select("Occu188DiseaseTypeCode='D.Contrain'");
            if (rows1 != null && rows1.Length > 0)
            {
                table1 = rows1.CopyToDataTable();
            }

            table1.TableName = "职业禁忌证";
            this.SetTable(table1);
            this.fValuesToFill.Fields.Add(new FieldContent("职业健康检查汇总结果.ContraindicationsCount", table1.Rows.Count.ToString()));

            // 疑似职业病
            DataTable table2 = new DataTable();
            DataRow[] rows2 = dt.Select("Occu188DiseaseTypeCode='C.Disease'");
            if (rows2 != null && rows2.Length > 0)
            {
                table2 = rows2.CopyToDataTable();
            }

            table2.TableName = "疑似职业病";
            this.SetTable(table2);
            this.fValuesToFill.Fields.Add(new FieldContent("职业健康检查汇总结果.OccDiseasesCount", table2.Rows.Count.ToString()));

            // 复查
            DataTable table3 = new DataTable();
            DataRow[] rows3 = dt.Select("Occu188DiseaseTypeCode='B.ReExamine'");
            if (rows3 != null && rows3.Length > 0)
            {
                table3 = rows3.CopyToDataTable();
            }

            table3.TableName = "复查";
            this.SetTable(table3);
            this.fValuesToFill.Fields.Add(new FieldContent("职业健康检查汇总结果.ReviewCount", table3.Rows.Count.ToString()));

            // 未见异常
            DataTable table4 = new DataTable();
            DataRow[] rows4 = dt.Select("Occu188DiseaseTypeCode='A.Normal'");
            if (rows4 != null && rows4.Length > 0)
            {
                table4 = rows4.CopyToDataTable();
            }

            table4.TableName = "未见异常";
            this.SetTable(table4);
            this.fValuesToFill.Fields.Add(new FieldContent("职业健康检查汇总结果.NomalCount", table4.Rows.Count.ToString()));
        }

        public void SetTable(DataTable dt)
        {
            if (dt == null)
            {
                return;
            }

            TableContent table = new TableContent(dt.TableName);
            this.fValuesToFill.Tables.Add(table);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    List<FieldContent> rows = dt.Rows[i].DataRowToFields(dt.TableName);
                    table.AddRow(rows.ToArray());
                }
            }
            else
            {
                table.Hide();
            }
        }
    }

    /// <summary>
    /// 职检基本信息
    /// </summary>
    internal class OccHMInfo
    {
        /// <summary>
        /// 职检基本信息
        /// </summary>
        public Dictionary<string, string> DictOccHMInfo { get; set; }

        public OccHMInfo(DataTable dt)
        {
            if (dt != null)
            {
                this.DictOccHMInfo = new Dictionary<string, string>();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    DataColumn dc = dt.Columns[i];
                    if (dc.ColumnName == "DateReservation")
                    {
                        DateTime? time = dt.Rows[0].DataRowToDateTimeNull(dc.ColumnName);
                        this.DictOccHMInfo.Add($"{dt.TableName}.{dc.ColumnName}", time.HasValue ? time.Value.ToString("yyyy-MM-dd") : string.Empty);
                    }
                    else
                    {
                        this.DictOccHMInfo.Add($"{dt.TableName}.{dc.ColumnName}", dt.Rows[0].DataRowToString(dc.ColumnName));
                    }
                }
            }
        }
    }
}
